<html> 
	
<head> 
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<script language=javascript> 

 

var refreshTime = 10;//登录前
var loginTryCount = 1;//尝试登录次数
var oldUnreadCount = 0;
function goPage(surl)
{
 chrome.tabs.getAllInWindow(undefined, function(tabs) {
    for (var i = 0, tab; tab = tabs[i]; i++) { 
      if (tab.url==surl) {
        chrome.tabs.update(tab.id, {selected: true});
        return;
      }
    }  
	chrome.tabs.create({url:surl});
  });
}
function verifyLogin()
{
  var xhr = new XMLHttpRequest(); 
  xhr.open("GET", "http://api.t.sina.com.cn/account/verify_credentials.json?source=3900123176", true); 
  xhr.onreadystatechange =  
  function() 
  { 
    if (xhr.readyState == 4) 
	 {
	   var respText = xhr.responseText;	   
	   if(respText.indexOf('error_code')>=0)
	     {		  
		  chrome.browserAction.setTitle({title:"新浪微博插件\n状态：尝试登录\n请进入插件选项页面登录"});
          chrome.browserAction.setIcon({path:"img/coriander_off.png"}); 
          chrome.browserAction.setBadgeBackgroundColor({color:[190, 190, 190, 230]}); 
          chrome.browserAction.setBadgeText({text: localStorage.unreadCount==0?"?":localStorage.unreadCount});
		  loginTryCount--;
		  if(localStorage.uid==0&&loginTryCount>=0)goPage("options.html");//未登录微博跳转到设置界面登录	
		  }
	   else	     
	     {
		  var myObject = JSON.parse(respText); 
		  localStorage.uid=myObject.id; //以登录微博获取用户ID
		  localStorage.uname=myObject.name;
          xhr =  new XMLHttpRequest(); 
          xhr.open("GET", "http://api.t.sina.com.cn/statuses/friends_timeline.json?source=3900123176", true); 
          xhr.onreadystatechange = function()
		  {
		    if(xhr.readyState==4)
			{
             var myObject = JSON.parse(xhr.responseText); 
			 if(localStorage.maxId==0)localStorage.maxId=myObject[0].id;
			 refreshCount();
			}
		  };
		  xhr.send();
	    }
	 }
  };
  xhr.send(); 
}

function init()
{
  localStorage.pageNo=1;
  localStorage.uid=0;
  localStorage.uname="";
  localStorage.currentListUrlName="statuses/friends_timeline";
  localStorage.maxId=0;
  localStorage.unreadCount=0;
  oldUnreadCount=0;
  if(localStorage.loginedRefreshTime==null) localStorage.loginedRefreshTime=45;//登录后
  if(localStorage.fontSize==null)  localStorage.fontSize='10px';
  if(localStorage.theme==null) localStorage.theme='';//登录后
  if(localStorage.allowSound==null) localStorage.allowSound='true';//登录后
  if(localStorage.fontFamily==null) localStorage.fontFamily='Arial';
  if(localStorage.unfeed==null)
   {
  	   localStorage.unfeed='true';
	   localStorage.unfollow='true';
	   localStorage.unfans='true';
	   localStorage.uncomment='true';
	   localStorage.unmessage='true';
   }
  chrome.browserAction.setTitle({title:"新浪微博插件\n状态：尝试登录\n请进入插件选项页面登录"});
  chrome.browserAction.setIcon({path:"img/coriander_off.png"}); 
  chrome.browserAction.setBadgeBackgroundColor({color:[190, 190, 190, 230]}); 
  chrome.browserAction.setBadgeText({text: localStorage.unreadCount==0?"?":localStorage.unreadCount});
  //校验是否登录
  login(); 
}

function refreshCount()
{
  if(localStorage.uid!=0)refreshTime=localStorage.loginedRefreshTime;//登录后设置刷新时间
  var xhr = new XMLHttpRequest(); 
  var url =  "http://t.sina.com.cn/public/aj_count.php?uid="+localStorage.uid+"&r="+ Math.random();
  xhr.open("GET",url, true); 
  xhr.onreadystatechange =  
  function() 
  { 
    if (xhr.readyState == 4) 
	 {
        var myObject = JSON.parse(xhr.responseText); 
        localStorage.newReplyCount =myObject.data.comment>0?myObject.data.comment:0;		
		localStorage.newFansCount = myObject.data.attention.num>0?myObject.data.attention.num:0;
		localStorage.newMsgCount =  myObject.data.msg>0?myObject.data.msg:0; 
		localStorage.newAtmeCount = myObject.data.atme>0?myObject.data.atme:0;
        xhr =   new XMLHttpRequest(); 
        xhr.open("GET", "http://api.t.sina.com.cn/statuses/friends_timeline.json?source=3900123176&count=99&since_id="+localStorage.maxId, true); 
        xhr.onreadystatechange = function()
		{
		  if(xhr.readyState==4)
			{
			 var rs = xhr.responseText;
			 if(rs=='[]'||rs.indexOf("error_code")>0) localStorage.newFeedCount=0;
			 else
             {
				 var myObject = JSON.parse(rs); 
				 if(localStorage.maxId==0)localStorage.maxId=myObject[0].id;
				 localStorage.newFeedCount = myObject.length;
             }	
				 localStorage.unreadCount = 
				                   (localStorage.uncomment=='true'?parseInt(localStorage.newReplyCount):0)+
			                       (localStorage.unfans=='true'?parseInt(localStorage.newFansCount):0)+
								   (localStorage.unfeed=='true'?parseInt(localStorage.newFeedCount):0)+
								   (localStorage.unmessage=='true'?parseInt(localStorage.newMsgCount):0)+								 
								   (localStorage.unfollow=='true'?parseInt(localStorage.newAtmeCount):0);	
				
		        showTooltip();				  
			}
		 };
		xhr.send();       					  
	 } 
  };
  xhr.send(); 
}
function login()
{
    var xhr = new XMLHttpRequest(); 
    var url = "http://t.sina.com.cn/";
    xhr.open("GET",url, true); 
    xhr.onreadystatechange =  function() {
     if (xhr.readyState == 4) {	
	verifyLogin();
	 }};
  xhr.send();
}

function refresh()
{
   if(localStorage.uid==0)
    login();
	else
    refreshCount();
} 


function showTooltip()
{ 
  var myTwitterTitle = "Coriander-新浪微博插件\n"+
                           (localStorage.unfans=='true'?localStorage.newFansCount+"/新粉丝 ":"")+
                           (localStorage.uncomment=='true'?localStorage.newReplyCount+"/新评论 \n":"")+
                           (localStorage.unmessage=='true'? localStorage.newMsgCount+"/新消息 ":"")+
						   (localStorage.unfollow=='true'?localStorage.newAtmeCount+"/新@我\n":"")+
						   (localStorage.unfeed=='true'?localStorage.newFeedCount+"/新微博 ":"");
 var notificationBody = "Coriander-新浪微博插件     :"+
                           (localStorage.unfans=='true'&&localStorage.newFansCount>0?localStorage.newFansCount+"/新粉丝  ":"")+
                           (localStorage.uncomment=='true'&&localStorage.newReplyCount>0?localStorage.newReplyCount+"/新评论   ":"")+
                           (localStorage.unmessage=='true'&&localStorage.newMsgCount>0? localStorage.newMsgCount+"/新消息  ":"")+
						   (localStorage.unfollow=='true'&&localStorage.newAtmeCount>0?localStorage.newAtmeCount+"/新@我  ":"")+
						   (localStorage.unfeed=='true'&&localStorage.newFeedCount>0?localStorage.newFeedCount+"/新微博 ":"");
  chrome.browserAction.setTitle({title:myTwitterTitle});
  chrome.browserAction.setIcon({path:"img/coriander.png"});  
  chrome.browserAction.setBadgeBackgroundColor({color:[26,134,58,240]});   
  var  newAllCount =localStorage.unreadCount;

  chrome.browserAction.setBadgeText({text: newAllCount==0?"":newAllCount>9999?">N":newAllCount+""});

  if(parseInt(localStorage.unreadCount)>oldUnreadCount&&localStorage.notify=='true')
				  {
                  show(notificationBody);
				  oldUnreadCount=parseInt(localStorage.unreadCount);
				  }
}
  function show(notificationBody) {
    var time = /(..)(:..)/(Date());              // The prettyprinted time.
    var hour = time[1] % 12 || 12;               // The prettyprinted hour.
    var period = time[1] < 12 ? '上午' : '下午'; // The period of the day.
  var notification = webkitNotifications.createNotification(
  'img/coriander.png',  // icon url - can be relative
  hour + time[2] + ' ' + period,  // notification title
  notificationBody  // notification body text
  );
    notification.show();
  }
 //定义一个反复执行的调用 
 window.setInterval(refresh,refreshTime*1000);//1分钟循环



</script> 
</head> 
<body onload="init()"> 
<img id="logged_in" src="img/coriander.png"/> 
<canvas id="canvas" width="19" height="19"></canvas>
<!--audio id="notify_sound" src="notify.mp3" onclick='this.play()'></audio-->

</body> 
</html>